import Vue from 'vue'
import VueRouter from 'vue-router'
import { Notify } from 'vant';
Vue.use(VueRouter)

let secondRoutes = [
  {
    path: '/home',
    redirect: '/home/index',  // 重定向:重新指向其它path,会改变网址
  },
  {
    path: '/home/index',
    name: "Index",
    component: () => import("../views/home/second/index/Index.vue"),
    meta: {
      title: "首页",
      second: true,
      icon: "home-o"
    }
  },
  {
    path: '/home/classify',
    name: "Classify",
    component: () => import("../views/home/second/classify/Classify.vue"),
    meta: {
      title: "分类",
      second: true,
      icon: "apps-o"
    }
  },
  {
    path: '/home/shopcar',
    name: "Shopcar",
    component: () => import("../views/home/second/shopcar/Shopcar.vue"),
    meta: {
      title: "购物车",
      second: true,
      icon: "shopping-cart-o"
    }
  },
  {
    path: '/home/my',
    name: "My",
    component: () => import("../views/home/second/my/My.vue"),
    meta: {
      title: "我的",
      second: true,
      icon: "manager-o"
    }
  },
]

const routes = [
  {
    path: '/',
    redirect: '/home',  // 重定向:重新指向其它path,会改变网址
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import("../views/home/Home.vue"),
    title: "主页",
    children: secondRoutes
  },
  {
    path: '/details/:id',
    name: 'Details',
    component: () => import("../views/details/Details.vue"),
    title: "详情页",
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import("../views/login/Login.vue"),
    title: "登录页",
  },
  {
    path: '*',
    name: 'Error404',
    component: () => import("../views/Error/Error404.vue"),
    title: "没有找到页面",
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

const Administrator = {
  path: '/home/administrator',
  name: "Administrator",
  component: () => import("../views/home/second/administrator/Administrator.vue"),
  meta: {
    title: "管理员",
    second: true,
    icon: "setting-o"
  }
}
const token = window.localStorage.getItem("token");
if (token) {
  router.addRoute("Home", Administrator)
}

// 设置白名单
const whitePath = ["/login","/home/classify","/home/index","/home","/"]
router.beforeEach((to, from, next) => {
  if (token || whitePath.includes(to.path)) {
    next()
  }else{
    // Notify({
    //   message: '请登录',
    //   duration: 1000,
    // });
    next("/login")
  }
})

export default router
